package com.action;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.omg.CORBA.Request;

import com.dao.OrderInfo;
import com.dbserver.Condb;
import com.server.InfoService;
import com.server.OrderListService;

public class OrderList {
	//7.交易详情列表，可按日期、类别、商家、用户筛选
	public String GetOrderList(){ 
		HttpServletRequest request = ServletActionContext.getRequest ();
		String date="";
		int userid=-1;
		int type=-1;
		int shopid=-1;
		if(request.getParameter("userid")!=null)
		{
			date=request.getParameter("date");//日期格式:"yyyy-MM-dd"
			userid=Integer.valueOf(request.getParameter("userid"));//不按照userid的话默认userid=-1
			type=Integer.valueOf(request.getParameter("type"));//不按照type的话默认type=-1
			shopid=Integer.valueOf(request.getParameter("shopid"));//不按照shopid的话默认shopid=-1
		}
		
		
		
		String pageNumberStr = request.getParameter("pageNumber");
		int pageNumber = 1;  
		if(pageNumberStr!=null && !pageNumberStr.isEmpty())  
		{  
		        pageNumber = Integer.parseInt(pageNumberStr);  
		}  
		    
		int pageSize = 10; //分页大小  
		OrderListService orderListService=new OrderListService();
		List<OrderInfo> orderInfoList=orderListService.getOrder(date, userid, type, shopid, pageNumber, pageSize);
		
		int totalPosts=0; //问题总数  
	      //处理结果集
		  Condb condb=new Condb();
		  String sql="select count(o.id) from orderitem o inner join paservice p on o.paserviceid=p.id inner join shopinfo si on p.shopid=si.id";
		   	if(date!=""||userid!=-1||type!=-1||shopid!=-1)
		   	{
		   		sql+=" where";
		   		if(date!="")
			   	{
			   		sql+=" Date(ordertime)="+date;
			   		if(userid!=-1)
			   		{
			   			sql+=" and o.userid="+userid;
			   		}
			   		if(type!=-1)
			   		{
			   			sql+=" and p.type="+type;
			   		}
			   		if(shopid!=-1)
			   		{
			   			sql+=" and si.id="+shopid;
			   		}
			   	}else {
			   		if(userid!=-1)
			   		{
			   			sql+=" o.userid="+userid;
			   			if(type!=-1)
				   		{
				   			sql+=" and p.type="+type;
				   		}
				   		if(shopid!=-1)
				   		{
				   			sql+=" and si.id="+shopid;
				   		}
			   		}else {
			   			if(type!=-1)
				   		{
				   			sql+=" p.type="+type;
				   			if(shopid!=-1)
					   		{
					   			sql+=" and si.id="+shopid;
					   		}
				   		}else {
				   			if(shopid!=-1)
					   		{
					   			sql+=" si.id="+shopid;
					   		}
						}
				   		
					}
			   		
				}
		   		
		   	}
		  ResultSet rs=condb.query(sql);
		  try {
				if(rs.next())
				{
					totalPosts=rs.getInt(1);
				}
				rs.close();
				condb.close();
		  } catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
	      }
		  int totalPages = totalPosts/pageSize + ((totalPosts%pageSize)>0?1:0); //计算得出的总页数  
		  
		   //下拉列表的值
		   InfoService serviceTypeService=new InfoService();
		   Map typeM=serviceTypeService.getType();
		   request.setAttribute("typeS", typeM);
		   
		   Map shopMap=serviceTypeService.getShop();
		   request.setAttribute("shop", shopMap);
		   
		   Map userM=serviceTypeService.getUser();
		   request.setAttribute("user", userM);
		   
		  request.setAttribute("date", date);
		  
		  request.setAttribute("type", type);
		  request.setAttribute("shopid", shopid);
		  request.setAttribute("userid", userid);
		  request.setAttribute("pageSize", pageSize);  
	      request.setAttribute("totalPosts", totalPosts);  
	      request.setAttribute("pageNumber", pageNumber);  
	      request.setAttribute("totalPages", totalPages);  
	      request.setAttribute("entryList", orderInfoList); 
		  return "success";
	}
}
